<?php
	require_once './handler/check_session.php';	
	require_once './data/Dossier.php';
	
	$nb_creation = $banque->getNbDossierEnDemande();
	$nb_encours = $banque->getNbDossierEnCour();
	$nb_emis = $banque->getNbDossierEmis();
	
	$NB_DOSSIER = 15;
	$filter="";
	$page = 0;
	$order = 'date';
	$wayorder = 'desc';
	$filter_statut=-1;
	$filter_banque=-1;
	
	if(isset($_GET['nameFilter'])){
		$filter = $_GET['nameFilter'];
	}
	
	if(isset($_GET['statusFilter'])){
		$filter_statut = $_GET['statusFilter'];
	}
	
	if(isset($_GET['banqueFilter'])){
		$filter_banque = $_GET['banqueFilter'];
	}
	
	if(isset($_GET['page'])){
		$page = $_GET['page'];
	}
	
	if(isset($_GET['order'])){
		$order = $_GET['order'];
	}
	
	if(isset($_GET['wayorder'])){
		$wayorder = $_GET['wayorder'];
	}
	
	$tab_statut = array("Demande Emise","Attente Client","Analyse Assurance","Complément Médical","Acceptation Client","Contrat Emis","Demande Résiliation","Contrat Résilié");
	$moisDernier = array("décembre", "janvier", "février", "mars", "avril", "mai", "juin", "juillet","août", "septembre", "octobre", "novembre");
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-US" lang="en-US">
    <head>
        <title>Consultation</title>
        <meta http-equiv='Content-Type' content='text/html; charset=utf-8'/>
        <link rel="STYLESHEET" type="text/css" href="style/header_footer_style.css" />
        <link rel="STYLESHEET" type="text/css" href="style/main_style.css" />
        <script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
        <script type='text/javascript'>
	        function go(page){
	        	document.getElementById("pageText").innerHTML = "chargement...";
	        	var filtName = document.getElementById("nameFilter").value;
	        	var statusFilter = document.getElementById("statusFilter").value;
	        	var banqueFilter = document.getElementById("banqueFilter") != null ? document.getElementById("banqueFilter").value : "";
	        	$("#main2").load('main.php?page='+page + "&nameFilter=" + filtName + "&statusFilter=" + statusFilter + "&banqueFilter=" + banqueFilter + " #main2"); 
	        }

	        function changeOrder(order, oldOrder, oldwayorder)
	        {
	        	document.getElementById("pageText").innerHTML = "chargement...";
	        	var filtName = document.getElementById("nameFilter").value;
	        	var statusFilter = document.getElementById("statusFilter").value;
	        	var banqueFilter = document.getElementById("banqueFilter") != null ? document.getElementById("banqueFilter").value : "";

				var wayorder = "asc";

				if(order == oldOrder && oldwayorder == "asc")
				{
					wayorder = "desc";
				}
	        	
	        	$("#main2").load('main.php?wayorder='+wayorder+'&order='+order+'&page=0&nameFilter=' + filtName + "&statusFilter=" + statusFilter + "&banqueFilter=" + banqueFilter + " #main2"); 
			}
        </script>
    </head>
    <!-- BODY -->
    <body>
    	<!-- HEADER -->
        <?php include_once'./include/header.php'; ?>
		<div id="defilement" ><marquee id="text_defil" scrollamount="3" >
			<span onmouseover="getElementById('text_defil').stop();" onmouseout="getElementById('text_defil').start();">
				Rappel : en <?php echo(ucwords($moisDernier[date('m')-1])); ?>, il y a eu <?php echo $nb_creation; ?> demande(s) réalisée(s), <?php echo $nb_encours; ?> dossier(s) en cours d'analyse et <?php echo $nb_emis; ?> contrat(s) emis.
			</span>
		</marquee></div> 
        <!-- MAIN -->
		<div id="main" class="consultation">
			<form id="searchForm" action="javascript:go(<?php echo $page; ?>)" method="post"  accept-charset="UTF-8">
                <table id="searchFilter">
                	<tr>
	                    <td align="left">
	                        Filtrer par <b>Mots cl&eacute; :</b><br/>
	                        <input type='text' name='nameFilter' id='nameFilter' value='<?php echo $filter; ?>' size="50%" />
	                     </td>                    

	                    <td>
	                        <label for='statusFilter' >Filtrer par <b>STATUT</b></label><br/>
	                        <select name="statusFilter" id='statusFilter' size="1">
	                            <option value=-1></option>
	                            <option value=0>Demande Emise</option>
	                            <option value=1>Attente Client</option>
	                            <option value=2>Analyse Assurance</option>
	                            <option value=3>Complement M&eacute;dical</option>
	                            <option value=4>Acceptation Client</option>
	                            <option value=5>Contrat Emis</option>
	                            <option value=6>Demande R&eacute;siliation</option>
	                            <option value=7>Contrat R&eacute;sili&eacute;</option>
	                        </select>
	                       
	                    </td> 
	                  
	                    
	                  	<?php if($_SESSION['user'] == 'admin'){ ?>
	                    <td>
	                        <label for='banqueFilter' >Filtrer par <b>BANQUE</b></label><br/>
	                        <select name="banqueFilter" id='banqueFilter' size="1">
	                            <option value=-1></option>
	                            
	                            <?php

	                            $list_banque = Banque::getListAllBanque();
	                            
	                             for ($index = 0; $index < count($list_banque); $index++) {
									$banq = $list_banque[$index]; ?>
	                            <option value=<?php echo $banq->id; ?>><?php echo $banq->nom; ?></option>
	     					   <?php } ?>
	                        </select>
	                    </td>
	                    <?php } ?>
	                    
	                      <td>
	                    <br/>
	                     <input type='button' onclick="javascript:go(<?php echo $page; ?>)" value="Rechercher" />
	                    </td>
                    </tr>                
                </table>
			</form>
			
			<?php   
				
				//set order by value
				Dossier::setOrder($order, $wayorder);
				
				if($_SESSION['user'] == 'admin'){
					$result = Dossier::getListAllDossier();	
					if($filter != "" & $filter_statut != -1 ){
						$result = Dossier::getListDossierByKeyAndStatut($filter, $filter_statut, $filter_banque);
					}else  if($filter_statut != -1){
						$result = Dossier::getListDossierByStatut($filter_statut, $filter_banque);
					}else if($filter != "" || ($filter == "" & $filter_banque != -1)){
						$result = Dossier::getListDossierByKey($filter, $filter_banque);
					}
				}else{
					$result = Dossier::getListDossierByBanque($banque->id); 
					$id_banque = $banque->id;
					if($filter != "" & $filter_statut != -1 ){
						$result = Dossier::getListDossierByKeyAndStatut($filter, $filter_statut, $id_banque);
					}else if($filter != ""){
						$result = Dossier::getListDossierByKey($filter, $id_banque);
					}else  if($filter_statut != -1){
						$result = Dossier::getListDossierByStatut($filter_statut, $id_banque);
					}
				}
			?>
			<br/>
			<div id="main2">
			<table id="resultTab">
				<tr class='header'>
				<th style="width: 100px;"><a href="javascript:changeOrder('date','<?php echo $order; ?>', '<?php echo $wayorder; ?>')">Date</a> </th>
				<th style="width: 125px;"><a href="javascript:changeOrder('nom','<?php echo $order; ?>', '<?php echo $wayorder; ?>')">Nom</a></th>
				<th style="width: 125px;"><a href="javascript:changeOrder('prenom','<?php echo $order; ?>', '<?php echo $wayorder; ?>')">Pr&eacute;nom</a></th>
				<th style="width: 100px;">Montant prêt</th>
				<th style="width: 200px;"><a href="javascript:changeOrder('date_changement_statut','<?php echo $order; ?>', '<?php echo $wayorder; ?>')">Dernier changement status</a></th>
				<th style="width: 125px;"><a href="javascript:changeOrder('statut','<?php echo $order; ?>', '<?php echo $wayorder; ?>')">Statut</a></th>
			</tr>
			<?php 
				// DEBUG
				$page_max = round (count($result)/$NB_DOSSIER,0);
				if($page_max == 0)
				$page_max = 1;
					
				if((count($result)/$NB_DOSSIER) > $page_max)
					$page_max++;
					
				//check page good ranges
				//
				if($page + 1 > $page_max)
					$page = $page_max -1;
					
				if($page < 0)
					$page = 0;
					
				$min = $page;
					
				//check min and max ranges
				if($min > 0)
					$min = $min * $NB_DOSSIER;
				
				if($min > count($result))
					$min = count($result) - ($min - count($result));
				
				$max = $page * $NB_DOSSIER + $NB_DOSSIER;
						 
				if($max >  count($result))
					$max =  count($result);

				for ($index = $min; $index < $max; $index++) {
					$dossier = $result[$index];
			?>
				<tr onclick="document.location.href='view_dossier.php?id=<?php echo $dossier->id; ?>'" onmouseover="this.className='highlight'" onmouseout="this.className='normal'" >
					<td style="width: 100px;"><?php echo $dossier->date; ?></td>
					<td style="width: 125px;"><?php echo $dossier->nom; ?></td>
					<td style="width: 125px;"><?php echo $dossier->prenom; ?></td>
					<td style="width: 100px;"><?php echo $dossier->getSumEmprunt(); ?> €</td>
					<td style="width: 200px;"><?php echo $dossier->date_changement_statut; ?></td>
					<td style="width: 125px;"><?php echo $tab_statut[$dossier->statut]; ?></td>
				</tr>
			<?php } ?>
			</table>
			<br/>
			<a href="javascript:go(<?php echo $page-1; ?>)" onclick="">   <img src="ressources/gauche.jpg"  border="0"/></a>
			<label id='pageText' > <?php echo ($page + 1)."/".$page_max; ?> </label>
			<a href="javascript:go(<?php echo $page+1; ?>)"> <img src="ressources/droite.png"  border="0"/> </a>
		</div>
	</div>
		<!-- FOOTER -->
        <?php include_once'./include/footer.php'; ?>
    </body>
</html>

        
        
               